<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/5/29
 * Time: 16:55
 */
namespace app\admin\controller;
use think\Request;
use think\Db;
class Village extends Base {
    public function index(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            if(isset($keyword)&&$keyword!="")
            {
                $map['a.xq_name'] = ['like',"%" . $keyword . "%"];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('xq')->alias('a')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('xq')->alias('a')
                ->where($map)
                ->page($NowPage,$limits)
                ->order('a.xq_id desc')
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(session('?xq_id')){
            session('xq_id',null);
        }
        return $this->fetch();
    }
    /***
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $task_id = input('param.id');
        $num = Db::table('up_xq_house')->where('xq_id',$task_id)->count();
        if($num>0){
            return json(['code'=>100,'msg'=>'小区下面又房产不能删除']);
        }else{
            $res = Db::name('xq')->where('xq_id',$task_id)->delete();
            if($res){
                return json(['code'=>200,'msg'=>'删除成功']);
            }else{
                return json(['code'=>100,'msg'=>'删除失败']);
            }
        }

    }
    /***
     * @return mixed|\think\response\Json
     * 添加
     */
    public function add(){
        if(Request::instance()->isPost()){
            $banner_title =input('xq_name');
            $banner_sort= input('xq_address');
            $data['xq_name'] = $banner_title;
            $data['xq_address'] = $banner_sort;
            $data['xq_time'] = date('Y-m-d H:i:s',time());
            $res = Db::table('up_xq')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }
    //物业管理员列表
    public function wy(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            $map['xw_xq_id'] =session('xq_id');
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('xq_wy_user')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.xw_user_id','left')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('xq_wy_user')->alias('a')
                ->where($map)
                ->join('up_u_user uuu','uuu.user_id = a.xw_user_id','left')
                ->page($NowPage,$limits)
                ->field('a.*,uuu.user_username,uuu.user_phone')
                ->order('a.xw_xq_id desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(!session('?xq_id')){
            session('xq_id',input('xq_id'));
        }
        return $this->fetch();
    }
    //添加小区物业管理员
    public function addition(){
        if(Request::instance()->isPost()){
            $id = input('param.id');//小区ID
            $xw_user_id =input('xw_user_id');
            foreach (explode(',',$xw_user_id) as $key=>$vo){
                $data['xw_xq_id'] = $id;
                $data['xw_user_id'] = $vo;
                $data['xw_time'] = date('Y-m-d H:i:s',time());
                $dataAll[]= $data;
                Db::table('up_u_user')->where('user_id',$vo)->setField('user_estate',2);
            }
            $res = Db::table('up_xq_wy_user')->insertAll($dataAll);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }

//        $where1['xq_id'] = session('xq_id');;
//        $where1['xq_sh_status'] = 2;
//        $info = Db::table('up_xq_property')->distinct(true)->where($where1)->column('xq_user_id');
//        $where['user_id'] =array('IN',$info);
        $id = input('xq_id');
        $where['user_estate'] =1;
        $res = Db::table('up_u_user')->where($where)->field('user_id,user_phone,user_username')->select();
        $this->assign('res',$res);
        $this->assign('id',$id);
        return $this->fetch();
    }
    //编辑
    public function update(){
        if(Request::instance()->isPost()){
            $id = input('param.id');//小区ID
            $xq_name = input('param.xq_name');//小区ID
            $xq_address= input('param.xq_address');//小区ID
            $dataInfo['xq_name'] =$xq_name;
            $dataInfo['xq_address'] =$xq_address;
            //先编辑小区
            $aa =Db::table('up_xq')->where('xq_id',$id)->update($dataInfo);
            //查询小区物业管理员表
            $cc = Db::table('up_xq_wy_user')->where('xw_xq_id',$id)->select();
            foreach ($cc as $kk=>$vv){
                Db::table('up_u_user')->where('user_id',$vv['xw_user_id'])->setField('user_estate',1);
            }
            //删除小区物业管理员
            $bb = Db::table('up_xq_wy_user')->where('xw_xq_id',$id)->delete();
            $xw_user_id =input('xw_user_id');
            foreach (explode(',',$xw_user_id) as $key=>$vo){
                $data['xw_xq_id'] = $id;
                $data['xw_user_id'] = $vo;
                $data['xw_time'] = date('Y-m-d H:i:s',time());
                $dataAll[]= $data;
                Db::table('up_u_user')->where('user_id',$vo)->setField('user_estate',2);
            }
            Db::table('up_xq_wy_user')->insertAll($dataAll);
            if($aa||$bb){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $id = input('xq_id');

        //查询这个小区的物业管理员
        $user = Db::table('up_xq_wy_user')->where('xw_xq_id',$id)->column('xw_user_id');
        //查询不是物业管理员的用户
        $where['user_estate'] =1;
        $where1['user_id'] =array('in',$user);
        $res = Db::table('up_u_user')->where($where)
            ->whereOr($where1)
            ->field('user_id,user_phone,user_username')->select();
        //c查询这个小区的数据
        $list = Db::table('up_xq')->where('xq_id',$id)->find();
        $this->assign('user',$user);
        $this->assign('res',$res);
        $this->assign('id',$id);
        $this->assign('list',$list);
        return $this->fetch();
    }



    //小区幢列表
    public function block(){
        if(request()->isAjax()){
            extract(input());
            if(isset($keyword)&&$keyword!="")
            {
                $map['a.block_name'] = ['like',"%" . $keyword . "%"];
            }
            $map['xq_id'] = session('xq_id');
            $map['is_del']= 1;
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('xq_block')->alias('a')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('xq_block')->alias('a')
                ->where($map)
                ->page($NowPage,$limits)
                ->order('a.block_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(session('?block_id')){
            session('block_id',null);
        }
        if(!session('?xq_id')){
            session('xq_id',input('xq_id'));
        }
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     * 删除
     */
    public function del1(){
        $task_id = input('param.id');
        $res = Db::name('xq_block')->where('block_id',$task_id)->setField('is_del',2);
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
    //添加幢
    public function add1(){
        if(Request::instance()->isPost()){
            $block_name =input('block_name');
            $data['block_name'] = $block_name;
            $data['xq_id'] = session('xq_id');
            $data['block_time'] = date('Y-m-d H:i:s',time());
            $res = Db::table('up_xq_block')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }
    //编辑小区(幢)
    public function update1(){
        if(Request::instance()->isPost()){
            $id = input('param.id');
            $block_name =input('block_name');
            $data['block_name'] = $block_name;
            $res = Db::table('up_xq_block')->where('block_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $id = input('block_id');
        $list = Db::table('up_xq_block')->where('block_id',$id)->find();
        $this->assign('id',$id);
        $this->assign('res',$list);
        return $this->fetch();
    }

    //小区幢列表
    public function unit(){
        if(request()->isAjax()){
            extract(input());
            if(isset($keyword)&&$keyword!="")
            {
                $map['a.unit_name'] = ['like',"%" . $keyword . "%"];
            }
            $map['blonk_id'] = session('block_id');
            $map['is_del']= 1;
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('xq_unit')->alias('a')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('xq_unit')->alias('a')
                ->where($map)
                ->page($NowPage,$limits)
                ->order('a.unit_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }

        if(!session('?block_id')){
            session('block_id',input('block_id'));
        }
        if(session('?unit_id')){
            session('unit_id',null);
        }
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     * 删除
     */
    public function del2(){
        $task_id = input('param.id');
        $res = Db::name('xq_unit')->where('unit_id',$task_id)->setField('is_del',2);
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
    //添加幢
    public function add2(){
        if(Request::instance()->isPost()){
            $block_name =input('unit_name');
            $data['unit_name'] = $block_name;
            $data['blonk_id'] = session('block_id');
            $data['unit_time'] = date('Y-m-d H:i:s',time());
            $res = Db::table('up_xq_unit')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }
    //编辑小区(幢)
    public function update2(){
        if(Request::instance()->isPost()){
            $id = input('param.id');
            $block_name =input('unit_name');
            $data['unit_name'] = $block_name;
            $res = Db::table('up_xq_unit')->where('unit_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $id = input('unit_id');
        $list = Db::table('up_xq_unit')->where('unit_id',$id)->find();
        $this->assign('id',$id);
        $this->assign('res',$list);
        return $this->fetch();
    }
    //小区幢列表
    public function room(){
        if(request()->isAjax()){
            extract(input());
            if(isset($keyword)&&$keyword!="")
            {
                $map['a.room_name'] = ['like',"%" . $keyword . "%"];
            }
            $map['unit_id'] = session('unit_id');
            $map['is_del']= 1;
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('xq_room')->alias('a')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('xq_room')->alias('a')
                ->where($map)
                ->page($NowPage,$limits)
                ->order('a.room_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }

        if(!session('?unit_id')){
            session('unit_id',input('unit_id'));
        }
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     * 删除
     */
    public function del3(){
        $task_id = input('param.id');
        $res = Db::name('xq_room')->where('room_id',$task_id)->setField('is_del',2);
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
    //添加幢
    public function add3(){
        if(Request::instance()->isPost()){
            $block_name =input('room_name');
            $data['room_name'] = $block_name;
            $data['unit_id'] = session('unit_id');
            $data['room_time'] = date('Y-m-d H:i:s',time());
            $res = Db::table('up_xq_room')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }
    //编辑小区(幢)
    public function update3(){
        if(Request::instance()->isPost()){
            $id = input('param.id');
            $block_name =input('room_name');
            $data['room_name'] = $block_name;
            $res = Db::table('up_xq_room')->where('room_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $id = input('room_id');
        $list = Db::table('up_xq_room')->where('room_id',$id)->find();
        $this->assign('id',$id);
        $this->assign('res',$list);
        return $this->fetch();
    }
}